package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenlida
 * @date 2018/12/14 11:07
 * @description
 */
public class Code78 {
    public static void main(String[] args) {
        System.out.println(new Code78().subsets(new int[] {1, 2, 3}));
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        lists.add(new ArrayList<>());
        for (int num : nums) {
            lists = getNewList(lists, num);
        }
        return lists;
    }

    private List<List<Integer>> getNewList(List<List<Integer>> lists, int num) {
        List<List<Integer>> list = deepCopy(lists);
        List<List<Integer>> newList = lists.stream().peek(x -> x.add(num)).collect(Collectors.toList());
        newList.addAll(list);
        return newList;
    }

    private List<List<Integer>> deepCopy(List<List<Integer>> lists) {
        List<List<Integer>> newList = new ArrayList<>();
        for (List<Integer> list : lists) {
            List<Integer> temp = new ArrayList<>(list);
            newList.add(temp);
        }
        return newList;
    }

}
